@extends('layouts.application')

@section('content')
    <div class="admin-content">
        <div class="admin-content-body">
            <div class="am-cf am-padding am-padding-bottom-0">
                <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">商品分类管理</strong> /
                    <small>Product Categories Manage</small>
                </div>
            </div>

            <hr>

            @include('layouts.shared._flash')

            <div class="am-g">
                <div class="am-u-sm-12 am-u-md-6">
                    <div class="am-btn-toolbar">
                        <div class="am-btn-group am-btn-group-xs">
                            <a href="{{route('crm.category.create')}}" type="button"
                               class="am-btn am-btn-default"><span class="am-icon-plus"></span> 新增
                            </a>
                            <button type="button" class="show_all am-btn am-btn-primary"><span class="am-icon-arrows"></span>
                                展开所有
                            </button>
                        </div>
                    </div>
                </div>

            </div>

            <div class="am-g">
                <div class="am-u-sm-12">
                    <form class="am-form">
                        <table class="am-table am-table-striped am-table-hover table-main">
                            <thead>
                            <tr>
                                <th>编号</th>
                                <th>缩略图</th>
                                <th>分类名</th>
                                <th>分类文章</th>
                                <th>是否显示</th>
                                <th>排序</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>

                            @foreach($categories as $category)
                                <tr data-id="{{$category->id}}">
                                    <td>{{$category->id}}</td>
                                    <td>
                                        <img src="{{$category->thumb}}" alt="" style="width: 50px;height: 50px;">
                                    </td>
                                    <td>{{$category->name}}</td>
                                    <td></td>
                                    <td>
                                        {!! is_something($category,'is_show') !!}
                                    </td>
                                    <td>
                                        <input type="hidden" name="id[]" value="{{$category->id}}">
                                        <input type="text" class="sort_order" name="sort_order[]" value="{{$category->sort_order}}" style="width: 50px;">
                                    </td>
                                    <td>
                                        <div class="am-btn-toolbar">
                                            <div class="am-btn-group am-btn-group-xs">
                                                <a href="{{route('crm.category.edit',$category->id)}}" class="am-btn am-btn-default am-btn-xs am-text-secondary"><span
                                                            class="am-icon-pencil-square-o"></span> 编辑
                                                </a>
                                                {{--<a href="{{route('shop.category.destroy',$category->id)}}" data-method="delete" data-token="{{csrf_token()}}" data-confirm="确认删除吗?"--}}
                                                   {{--class="am-btn am-btn-default am-btn-xs am-text-danger am-hide-sm-only">--}}
                                                    {{--<span class="am-icon-trash-o"></span> 删除--}}
                                                {{--</a>--}}

                                                <button class="delete am-btn am-btn-default am-btn-xs am-text-danger am-hide-sm-only">
                                                    <span class="am-icon-trash-o"></span> 删除
                                                </button>
                                            </div>
                                        </div>
                                    </td>
                                </tr>

                                @foreach($category->children as $child)
                                    <tr data-id="{{$child->id}}" class="Xchild" style="display: none;">
                                        <td>{{$child->id}}</td>
                                        <td>
                                            <img src="{{$child->thumb}}" alt="" style="width: 50px;">
                                        </td>
                                        <td>
                                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {{$child->name}}
                                        </td>
                                        <td>
                                            {!! show_category_articles($child) !!}
                                        </td>
                                        <td>
                                            {!! is_something($child,'is_show') !!}
                                        </td>
                                        <td>
                                            <input type="hidden" name="id[]" value="{{$child->id}}">
                                            <input type="text" class="sort_order" name="sort_order[]" value="{{$child->sort_order}}" style="width: 50px;">
                                        </td>
                                        <td>
                                            <div class="am-btn-toolbar">
                                                <div class="am-btn-group am-btn-group-xs">
                                                    <a href="{{route('crm.category.edit',$child->id)}}" class="am-btn am-btn-default am-btn-xs am-text-secondary"><span
                                                                class="am-icon-pencil-square-o"></span> 编辑
                                                    </a>
                                                    {{--<a href="{{route('shop.category.destroy',$child->id)}}" data-method="delete" data-token="{{csrf_token()}}" data-confirm="确认删除吗?"--}}
                                                       {{--class="am-btn am-btn-default am-btn-xs am-text-danger am-hide-sm-only">--}}
                                                        {{--<span class="am-icon-trash-o"></span> 删除--}}
                                                    {{--</a>--}}

                                                    <button class="delete am-btn am-btn-default am-btn-xs am-text-danger am-hide-sm-only">
                                                        <span class="am-icon-trash-o"></span> 删除
                                                    </button>
                                                </div>
                                            </div>
                                        </td>
                                    </tr>
                                @endforeach

                            @endforeach

                            </tbody>
                        </table>

                        <hr/>
                    </form>
                </div>

            </div>
        </div>

        <footer class="admin-content-footer">
            <hr>
            <p class="am-padding-left">© 2014 AllMobilize, Inc. Licensed under MIT license.</p>
        </footer>

    </div>
@endsection
@section('js')
    <script>
        $(function () {
            //显示隐藏分类
            $('.show_all').click(function () {
                $('.Xchild').toggle();
            });

            //改变属性
            $('.change_attr').click(function () {
                var id = $(this).parents('tr').data("id");
                var attr = $(this).attr('data-attr');
                var _this = $(this);
                $.ajax({
                    type : 'PATCH',
                    url : '{{route('crm.category.change_attr')}}',
                    data : {id:id,attr:attr},
                    success : function () {
                        _this.toggleClass('am-icon-check am-icon-close');
                    }
                });
                return false;
            })

            //ajax单个删除
            $('.delete').click(function () {
                var id = $(this).parents('tr').data('id');
                var _this = $(this);
                $.ajax({
                    type : 'DELETE',
                    url : '/admin/crm/category/'+id,
                    data : {id:id},
                    success : function (info) {
                        if(info.status == 1){
                            alert(info.msg);
                            _this.parents('tr').fadeOut(400);
                        }else{
                            alert(info.msg);
                        }
                    }
                });
                return false;
            })

            //排序
            $('.sort_order').change(function () {
                var id = $(this).parents('tr').data('id');
                var sort_order = $(this).val();

                $.ajax({
                    type : 'PATCH',
                    url : '{{route('crm.category.sort_order')}}',
                    data : {id:id,sort_order:sort_order},
                    success : function () {
                        window.location.reload();
                    }
                })
            })
        })
    </script>
@endsection